package com.sunday.haowushop.ui.mine.baseinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.utils.UploadUtils;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.Member;
import com.sunday.haowushop.event.UpdateBaseInfo;
import com.sunday.haowushop.http.ApiClient;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/14.
 * <p>
 * 店铺信息
 */

public class ShopInfoActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.shop_name)
    TextView shopName;
    @Bind(R.id.menu_shop_name)
    LinearLayout menuShopName;
    @Bind(R.id.shop_logo)
    ImageView shopLogo;
    @Bind(R.id.menu_shop_logo)
    LinearLayout menuShopLogo;
    @Bind(R.id.shop_welcome_logo)
    ImageView shopWelcomeLogo;
    @Bind(R.id.menu_shop_welcome_logo)
    LinearLayout menuShopWelcomeLogo;
    @Bind(R.id.shop_wechat_logo)
    ImageView shopWechatLogo;
    @Bind(R.id.menu_shop_wechat)
    LinearLayout menuShopWechat;


    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_info);
        ButterKnife.bind(this);
        member = BaseApp.getInstance().getMember();
        initView();
    }

    private void initView() {
        shopName.setText(member.getShopName());
        if (!TextUtils.isEmpty(member.getShopLogo())) {
            Glide.with(mContext)
                    .load(member.getShopLogo())
                    .error(R.mipmap.ic_default)
                    .into(shopLogo);
        }
        if (!TextUtils.isEmpty(member.getShopImage())) {
            Glide.with(mContext)
                    .load(member.getShopImage())
                    .error(R.mipmap.ic_default)
                    .into(shopWelcomeLogo);
        }
        if (!TextUtils.isEmpty(member.getTwoCode())) {
            Glide.with(mContext)
                    .load(member.getTwoCode())
                    .error(R.mipmap.ic_default)
                    .into(shopWechatLogo);
        }
    }

    private final static int REQUEST_LOGO = 0x1111;
    private final static int REQUEST_WELCOME_LOGO = 0x1112;
    private final static int REQUEST_WECHAT = 0x1113;

    @OnClick({R.id.menu_shop_name,R.id.menu_shop_logo, R.id.menu_shop_welcome_logo, R.id.menu_shop_wechat})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_shop_name:
                intent = new Intent(mContext, EditInfoActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.menu_shop_logo:
                intent = new Intent(mContext, MultiImageSelectorActivity.class);
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
                startActivityForResult(intent, REQUEST_LOGO);
                break;
            case R.id.menu_shop_welcome_logo:
                intent = new Intent(mContext, MultiImageSelectorActivity.class);
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
                startActivityForResult(intent, REQUEST_WELCOME_LOGO);
                break;
            case R.id.menu_shop_wechat:
                intent=new Intent(mContext,UploadWechatCodeActivity.class);
                startActivity(intent);
//                intent = new Intent(mContext, MultiImageSelectorActivity.class);
//                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
//                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
//                startActivityForResult(intent, REQUEST_WECHAT);
                break;
        }
    }

    private ArrayList<String> mSelectPath = new ArrayList<>();
    private ArrayList<String> mSelectPath1 = new ArrayList<>();
    private ArrayList<String> mSelectPath2 = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_LOGO:
                mSelectPath.clear();
                mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                if (mSelectPath != null) {
                    String logo = ImageUtils.getCompressImagePath(mSelectPath.get(0));
                    Glide.with(mContext)
                            .load(new File(logo))
                            .placeholder(R.drawable.default_error)
                            .error(R.drawable.default_error)
                            .into(shopLogo);
                    uploadImg(UploadUtils.getRequestBody(logo), REQUEST_LOGO);
                }
                break;
            case REQUEST_WELCOME_LOGO:
                mSelectPath1.clear();
                mSelectPath1 = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                if (mSelectPath1 != null) {
                    String logo = ImageUtils.getCompressImagePath(mSelectPath1.get(0));
                    Glide.with(mContext)
                            .load(new File(logo))
                            .placeholder(R.drawable.default_error)
                            .error(R.drawable.default_error)
                            .into(shopWelcomeLogo);
                    uploadImg(UploadUtils.getRequestBody(logo), REQUEST_WELCOME_LOGO);
                }
                break;
            case REQUEST_WECHAT:
                mSelectPath2.clear();
                mSelectPath2 = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                if (mSelectPath2 != null) {
                    String logo = ImageUtils.getCompressImagePath(mSelectPath2.get(0));
                    Glide.with(mContext)
                            .load(new File(logo))
                            .placeholder(R.drawable.default_error)
                            .error(R.drawable.default_error)
                            .into(shopWechatLogo);
                    uploadImg(UploadUtils.getRequestBody(logo), REQUEST_WECHAT);
                }
                break;
        }
    }

    private void uploadImg(final RequestBody requestBody, final int type) {
        showLoadingDialog(0);
        Call<ResultDO<List<String>>> call
                = ApiClient.getApiAdapter().uploadImgs(requestBody);
        call.enqueue(new Callback<ResultDO<List<String>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<String>>> call, Response<ResultDO<List<String>>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body() != null && response.body().getCode() == 0) {
                    //ToastUtils.showToast(mContext,"上传成功");
                    List<String> list = new ArrayList<String>();
                    list.addAll(response.body().getResult());
                    update(list.get(0), type);
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<String>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private void update(String logo, int type) {
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = null;
        switch (type) {
            case REQUEST_LOGO:
                call = ApiClient.getApiAdapter().saveEditInfo(member.getId(),null,logo, null,
                        null, null);
                break;
            case REQUEST_WELCOME_LOGO:
                call = ApiClient.getApiAdapter().saveEditInfo(member.getId(), null, null,logo,
                        null, null);
                break;
            case REQUEST_WECHAT:
                call = ApiClient.getApiAdapter().saveEditInfo(member.getId(), null, null, null,
                        null, logo);
                break;
        }
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getResult() != null && response.body().getCode() == 0) {
                    SharePerferenceUtils.getIns(mContext).saveOAuth(response.body().getResult());
                    BaseApp.getInstance().setMember(response.body().getResult());
                    member=response.body().getResult();
                    initView();
                }

            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
            }
        });
    }


    public void onEvent(UpdateBaseInfo updateBaseInfo) {
        member = BaseApp.getInstance().getMember();
        initView();
    }


}
